<?php
require_once 'header.php';

// 检查是否已登录
if (!isLoggedIn()) {
    $_SESSION['flash_message'] = '请先登录再进行结算';
    $_SESSION['flash_message_type'] = 'warning';
    header('Location: login.php');
    exit;
}

// 获取用户购物车
$user_id = $_SESSION['user_id'];
$cart_items = getUserCart($user_id);
$cart_total = getCartTotal($cart_items);

// 如果购物车为空，重定向到购物车页面
if (count($cart_items) === 0) {
    $_SESSION['flash_message'] = '您的购物车是空的，无法进行结算';
    $_SESSION['flash_message_type'] = 'warning';
    header('Location: cart.php');
    exit;
}

// 处理订单提交
$order_placed = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['place_order'])) {
    $name = $_POST['name'] ?? '';
    $phone = $_POST['phone'] ?? '';
    $address = $_POST['address'] ?? '';
    $payment_method = $_POST['payment_method'] ?? '';
    
    // 这里应该有更多的验证和实际的订单处理逻辑
    // 但为了演示，我们只是设置一个标志
    $order_placed = true;
    
    if ($order_placed) {
        // 清空购物车（实际应用中应该转移到订单表）
        $sql = "DELETE FROM cart WHERE user_id = ?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("i", $user_id);
        $stmt->execute();
        
        $_SESSION['flash_message'] = '订单已成功提交！感谢您的购买';
        $_SESSION['flash_message_type'] = 'success';
        
        header('Location: index.php');
        exit;
    }
}
?>

<div class="container">
    <h1 class="mb-4">结算</h1>
    
    <div class="row">
        <div class="col-md-8 mb-4">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">收货人信息</h5>
                </div>
                <div class="card-body">
                    <form action="checkout.php" method="post">
                        <div class="form-group">
                            <label for="name">姓名</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>
                        <div class="form-group">
                            <label for="phone">手机号码</label>
                            <input type="tel" class="form-control" id="phone" name="phone" required>
                        </div>
                        <div class="form-group">
                            <label for="address">详细地址</label>
                            <textarea class="form-control" id="address" name="address" rows="3" required></textarea>
                        </div>
                        
                        <hr class="my-4">
                        
                        <h5 class="mb-3">支付方式</h5>
                        <div class="d-block my-3">
                            <div class="custom-control custom-radio">
                                <input id="credit" name="payment_method" type="radio" class="custom-control-input" value="credit" checked required>
                                <label class="custom-control-label" for="credit">信用卡</label>
                            </div>
                            <div class="custom-control custom-radio">
                                <input id="alipay" name="payment_method" type="radio" class="custom-control-input" value="alipay" required>
                                <label class="custom-control-label" for="alipay">支付宝</label>
                            </div>
                            <div class="custom-control custom-radio">
                                <input id="wechatpay" name="payment_method" type="radio" class="custom-control-input" value="wechatpay" required>
                                <label class="custom-control-label" for="wechatpay">微信支付</label>
                            </div>
                        </div>
                        
                        <hr class="my-4">
                        
                        <button class="btn btn-primary btn-lg btn-block" type="submit" name="place_order" value="1">
                            提交订单
                        </button>
                    </form>
                </div>
            </div>
        </div>
        
        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">订单摘要</h5>
                </div>
                <div class="card-body">
                    <ul class="list-group list-group-flush mb-3">
                        <?php foreach($cart_items as $item): ?>
                        <li class="list-group-item d-flex justify-content-between lh-condensed">
                            <div>
                                <h6 class="my-0"><?php echo $item['name']; ?></h6>
                                <small class="text-muted">数量: <?php echo $item['quantity']; ?></small>
                            </div>
                            <span class="text-muted">¥<?php echo number_format($item['price'] * $item['quantity'], 2); ?></span>
                        </li>
                        <?php endforeach; ?>
                        <li class="list-group-item d-flex justify-content-between">
                            <span>总计</span>
                            <strong class="text-danger">¥<?php echo number_format($cart_total, 2); ?></strong>
                        </li>
                    </ul>
                    
                    <a href="cart.php" class="btn btn-outline-secondary btn-block">
                        <i class="fas fa-arrow-left mr-2"></i>返回购物车
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<?php require_once 'footer.php'; ?> 